#!/bin/bash
#
#cd ./Redis_init


NEW_REDIS_BIN_DIR=$(pwd)/../../bin/RedisServer
NEW_REDIS_DATA_DIR=$(pwd)/../../DataPacket/RedisData

PIDFILE=${NEW_REDIS_DATA_DIR}/redis_${REDISPORT}.pid
echo $PIDFILE

CONF="${NEW_REDIS_BIN_DIR}/redis.conf"

REDISPORT=$(sed -n '/^port/p' ${CONF}|awk -F" " '{print $NF}')
# 读取端口，读不到就设置默认值6379
if test -z ${REDISPORT}
then
    REDISPORT=6379
fi


ps -fe | grep redis-server |grep -v grep
if [ $? -eq 0 ]
 then
  echo "Stopping redis server..."
  svcadm disable redis
  svccfg delete -f redis
  echo "Redis stopped"
fi

REDIS_SERVER="$(svcs | grep redis)"

REDIS_TITLE=${REDIS_SERVER##*/}

echo $REDIS_TITLE

if [ "${REDIS_TITLE%:*}" = "redis" ] ; 
then
	echo "Stopping redis server..."
	svcadm disable redis
	svccfg delete -f redis
	echo "Redis stopped"
fi

if [ -f $PIDFILE ]; then
	rm -f ${PIDFILE}
fi

perl -i -pe "s:^REDIS_BIN_DIR=.*$:REDIS_BIN_DIR=${NEW_REDIS_BIN_DIR}:g" redis_script_solaris
perl -i -pe "s:^REDIS_DATA_DIR=.*$:REDIS_DATA_DIR=${NEW_REDIS_BIN_DIR}:g" redis_script_solaris

perl -i -pe "s:^dir.*$:dir $NEW_REDIS_DATA_DIR:g" $CONF
cp -f redis_script_solaris /etc/init.d/redis
chmod +x /etc/init.d/redis

REDIS_XML_DIR=/var/svc/manifest/application

cp -f redis.xml ${REDIS_XML_DIR}/

REDIS_SERVER="$(svcs | grep redis)"

REDIS_TITLE=${REDIS_SERVER##*/}
echo $REDIS_TITLE

if [ "${REDIS_TITLE%:*}" = "redis" ] ; 
then
	echo "redis has been existed in SMF"
	svcadm enable redis
	svcadm restart redis
else
	echo "now add redis server"
	svccfg import ${REDIS_XML_DIR}/redis.xml
	svcadm enable svc:/application/redis:default
fi

